package com.springStudy.beans.factory.support.defaultListableBeanFactory;

import org.junit.Test;
import org.springframework.beans.SimpleTypeConverter;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

import java.lang.reflect.Field;
import java.util.LinkedHashSet;
import java.util.Set;

public class DefaultListableBeanFactoryTest {
    public static void main(String[] args) {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
//        beanFactory.
    }

    /**
     * DefaultListableBeanFactory 注册一个Bean信息
     */
    @Test
    public void testRegisterBeanDefinition(){
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        Class serviceAC = ServiceA.class;
        Class serviceBC = ServiceB.class;
        RootBeanDefinition serviceABeanDefinition = new RootBeanDefinition(serviceAC);
        beanFactory.registerBeanDefinition("serviceA",serviceABeanDefinition );

        RootBeanDefinition serviceBBeanDefinition = new RootBeanDefinition(serviceBC);
        beanFactory.registerBeanDefinition("serviceB",serviceABeanDefinition );


//        beanFactory.getMergedBeanDefinition("serviceA")
//        Field[] declaredFields = serviceAC.getDeclaredFields();
//        for (int i = 0; i < declaredFields.length; i++) {
//
//            DependencyDescriptor desc = new DependencyDescriptor(declaredFields[i], true);
//            Set<String> autowiredBeanNames = new LinkedHashSet<>(1);
//            Object value = beanFactory.resolveDependency(desc, "serviceA", autowiredBeanNames, new SimpleTypeConverter());
//            serviceABeanDefinition.registerExternallyManagedConfigMember(declaredFields[i]);
//        }



        Object serviceA = beanFactory.getBean("serviceA");
        System.out.println(serviceA.getClass().getSimpleName()+" "+ serviceA.hashCode());
        serviceA = beanFactory.getBean("serviceA");
        System.out.println(serviceA.getClass().getSimpleName()+" "+ serviceA.hashCode());

    }
}
